<?php

class NewsController extends BaseController
{

	/**
	 * 发布资讯
	 */
	public function actionCreate()
	{
		$model=new News;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['News']))
		{
			$model->attributes=$_POST['News'];
			
			//分类
			if($_POST['cateid3']>0){
				$model->znc_id = (int)$_POST['cateid3'];
			}elseif($_POST['cateid2']>0){
				$model->znc_id = (int)$_POST['cateid2'];
			}elseif($_POST['cateid1']>0){
				$model->znc_id = (int)$_POST['cateid1'];
			}else{
				//失败跳转
				$this->render('../index/error',array('message'=>'分类不能为空，请选择分类！'));
				exit;
			}
			
			if($model->save()){
				//成功跳转
				$this->render('../index/success',array(  
			            'message'=>'恭喜，信息发布成功',  
			            'links'=>array(  
			            		array('资讯管理页面',$this->pre_module_url.'/news/admin'),
 			                    array('继续发布信息',$this->pre_module_url.'/newsCreate/index'),  
			                    array('浏览发布的信息','/home/news/detail/id/'.$model->zne_id),  
			             ), 
				));
				exit;
			}
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * 发布视频
	 */
	public function actionCreateVideo()
	{
		$model=new News;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['News']))
		{
			$model->attributes=$_POST['News'];
			//分类ID等于0为未选择
			if($model->znc_id==0){
				$model->znc_id = '';
			}
			if($model->save()){
				//成功跳转
				$this->render('../index/success',array(  
			            'message'=>'恭喜，视频发布成功',  
			            'links'=>array(  
			            		array('资讯管理页面',$this->pre_module_url.'/news/admin'),
 			                    array('继续发布信息',$this->pre_module_url.'/news/createvideo'),  
			                    array('浏览发布的信息','/home/news/detail/id/'.$model->zne_id),  
			             ), 
				));
				exit;
			}
		}

		$this->render('createvideo',array(
			'model'=>$model,
		));
	}

	/**
	 * 发布图库
	 */
	public function actionCreatePhoto()
	{
		$model=new News;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);		
		if(isset($_POST['News']))
		{
			$model->attributes=$_POST['News'];
			$model->zne_photo = serialize($_POST['News']['zne_photo']);
			//print_r($_POST['News']['zne_photo']);exit;
			foreach ($_POST['News']['zne_photo'] as $key => $value) {
				//写入附件表
				$attach=new Attachment();
				$attach->zat_url         = $value;//重新赋值 
			    $attach->zat_file_name   = $_POST['News']['zne_title'];           //文件名称           
			    $attach->zat_title       = $_POST['News']['zne_title'];           //文件标题
			    $attach->zat_description = $_POST['News']['zne_title'];           //文件描述
			    $attach->zat_file_type   = 'image/jpeg';                          //文件类型
			    $attach->zat_file_size   = filesize(Yii::app()->basePath.'/..'.$value); //文件大小			    
			    $attach->zat_image       = 2;                                     //是图像				
			    $attach->zat_ip          = Yii::app()->request->userHostAddress;  //上传IP
			    $attach->save();
			}
			//exit;
			//分类ID等于0为未选择
			if( $model->znc_id==0 ){
				$model->znc_id = '';
			}
			if($model->save()){
				//成功跳转
				$this->render('../index/success',array(  
			            'message'=>'恭喜，图库发布成功',  
			            'links'=>array(  
			            		array('资讯管理页面',$this->pre_module_url.'/news/admin'),
 			                    array('继续发布图库',$this->pre_module_url.'/news/createphoto'),  
			                    array('浏览发布的信息','/home/news/detail/id/'.$model->zne_id),  
			             ), 
				));
				exit;
			}
		}

		$this->render('createphoto',array(
			'model'=>$model,
		));
	}

	/**
	 *修改信息
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['News']))
		{
			$model->attributes= $_POST['News'];
			//行业分类
			if($_POST['cateid3']>0){
				$model->znc_id = (int)$_POST['cateid3'];
			}elseif($_POST['cateid2']>0){
				$model->znc_id = (int)$_POST['cateid2'];
			}elseif($_POST['cateid1']>0){
				$model->znc_id = (int)$_POST['cateid1'];
			}else{
				//失败跳转
				$this->render('../index/error',array('message'=>'行业分类不能为空，请选择行业分类！'));
				exit;
			}
			//合并两个数组
			if($_POST['News']['zne_photo'] ==''){
				$zne_photo        = $_POST['zne_photo'];
			}else{
				$zne_photo        = array_merge($_POST['zne_photo'], $_POST['News']['zne_photo']);
			}
			//序列化图库
			$model->zne_photo = serialize($zne_photo);
			//将新的图片写入附件表
			foreach ( (array)$_POST['News']['zne_photo'] as $key => $value) {
				//写入附件表
				$attach=new Attachment();
				$attach->zat_url         = $value;//重新赋值 
			    $attach->zat_file_name   = $_POST['News']['zne_title'];           //文件名称           
			    $attach->zat_title       = $_POST['News']['zne_title'];           //文件标题
			    $attach->zat_description = $_POST['News']['zne_title'];           //文件描述
			    $attach->zat_file_type   = 'image/jpeg';                          //文件类型
			    $attach->zat_file_size   = filesize(Yii::app()->basePath.'/..'.$value); //文件大小			    
			    $attach->zat_image       = 2;                                     //是图像				
			    $attach->zat_ip          = Yii::app()->request->userHostAddress;  //上传IP
			    $attach->save();
			}
			if($model->save()){
				//成功跳转
				$this->redirect(array('admin'));
			}
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * 单选，批量删除信息
	 */
	public function actionDelete()
	{
		//操作ID号格式处理
		if(isset($_GET['id'])){
			$id = $_GET['id'];
		}elseif(isset($_POST['ids'])){
			$id =$_POST['ids'];
		}else{
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		}
		$id = is_array($id)?join(',',$id):(int)$id;
		//处理条件
		$criteria=new CDbCriteria;
		$criteria->addCondition("zne_id in ($id)");
		//修改内容
		$set = array('zne_status'=>'3');
		//执行操作
		if(News::model()->updateAll($set,$criteria)){
			//成功跳转
			$this->redirect(Yii::app()->request->urlReferrer);
		}
	}


	/**
	 * 信息管理.
	 */
	public function actionAdmin()
	{
		$model=new News('search');
		$model->unsetAttributes();  // clear any default values
		$model->zne_status=array(1,2);//筛选已删除的信息
		if(isset($_GET['News'])){
			$model->attributes=$_GET['News'];
			//分类
			if($_GET['cateid3']>0){
				$model->znc_id = (int)$_GET['cateid3'];
			}elseif($_GET['cateid2']>0){
				$model->znc_id = (int)$_GET['cateid2'];
			}elseif($_GET['cateid1']>0){
				$model->znc_id = (int)$_GET['cateid1'];
			}
		}

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * 加载模型
	 */
	public function loadModel($id)
	{
		$model=News::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='news-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
	
	/**
	 *单条，批量设为头条
	 */
	public function actionRefocus(){
		//操作ID号格式处理
		if(isset($_GET['id'])){
			$id = $_GET['id'];
		}elseif(isset($_POST['ids'])){
			$id =$_POST['ids'];
		}else{
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		}
		$id = is_array($id)?join(',',$id):(int)$id;
		//处理条件
		$criteria=new CDbCriteria;
		$criteria->addCondition("zne_id in ($id)");
		//修改内容
		$set = array('zne_focus'=>'2');
		//执行操作
		if(News::model()->updateAll($set,$criteria)){
			//成功跳转
			$this->redirect(Yii::app()->request->urlReferrer);
		}
	}
	
	/**
	 *单条，批量取消头条
	 */
	public function actionUnfocus(){
		//操作ID号格式处理
		if(isset($_GET['id'])){
			$id = $_GET['id'];
		}elseif(isset($_POST['ids'])){
			$id =$_POST['ids'];
		}else{
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		}
		$id = is_array($id)?join(',',$id):(int)$id;
		//处理条件
		$criteria=new CDbCriteria;
		$criteria->addCondition("zne_id in ($id)");
		//修改内容
		$set = array('zne_focus'=>'1');
		//执行操作
		if(News::model()->updateAll($set,$criteria)){
			//成功跳转
			$this->redirect(Yii::app()->request->urlReferrer);
		}
	}
	
	/**
	 *单条，批量信息推荐
	 */
	public function actionRecommend(){
		//操作ID号格式处理
		if(isset($_GET['id'])){
			$id = $_GET['id'];
		}elseif(isset($_POST['ids'])){
			$id =$_POST['ids'];
		}else{
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		}
		$id = is_array($id)?join(',',$id):(int)$id;
		//处理条件
		$criteria=new CDbCriteria;
		$criteria->addCondition("zne_id in ($id)");
		//修改内容
		$set = array('zne_commend'=>'2');
		//执行操作
		if(News::model()->updateAll($set,$criteria)){
			//成功跳转
			$this->redirect(Yii::app()->request->urlReferrer);
		}
	}
	
	/**
 	 *单条，批量取消推荐
	 */
	public function actionUncommend(){
		//操作ID号格式处理
		if(isset($_GET['id'])){
			$id = $_GET['id'];
		}elseif(isset($_POST['ids'])){
			$id =$_POST['ids'];
		}else{
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		}
		$id = is_array($id)?join(',',$id):(int)$id;
		//处理条件
		$criteria=new CDbCriteria;
		$criteria->addCondition("zne_id in ($id)");
		//修改内容
		$set = array('zne_commend'=>'1');
		//执行操作
		if(News::model()->updateAll($set,$criteria)){
			//成功跳转
			$this->redirect(Yii::app()->request->urlReferrer);
		}
	}

	public function actionUnimage()
	{
		$pic = $_POST['pic'];
		if( !empty($pic) )
		{			
			Attachment::model()->deleteAll("zat_url = '".$pic."'");
			return Tool::delUpload($pic);

		}else{
			return 0;
		}		

	}
	
	
}
